package eighthDay;

public class ReverseList {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode left = head;
        ListNode mid = head.next;
        if (head.next.next == null){
            left.next = null;
            mid.next = left;
            return mid;
        }
        left.next = null;
        ListNode right = mid.next;
        if (right.next == null){
            right.next = mid;
            mid.next = left;
            return right;
        }
        while (right.next != null){
            mid.next = left;
            left = mid;
            mid = right;
            right = right.next;
        }
        mid.next = left;
        right.next = mid;
        return right;
    }
}
